﻿Imports System.Data.SqlClient
Public Class startistic

    Dim cmd As New SqlCommand
#Region "Code For SUM Total money"
    Public Sub totalAmount()
        cmd.Connection = cn
        cmd.CommandText = "select count (*) from tblinvoice where Date between '" & startdate.Value & "' and  '" & enddate.Value & "'"
        Dim count As Object
        count = cmd.ExecuteScalar
        txttotalinvoice.Text = count
        Try
            cmd.Connection = cn
            cmd.CommandText = " select SUM (Totalpay) from tblinvoice where Date between '" & startdate.Value & "' and  '" & enddate.Value & "'"
            txttotalmoney.Text = cmd.ExecuteScalar
        Catch ex As Exception
        End Try
    End Sub
#End Region

#Region "Select and refresh Data"
    Public Sub refreshdata()
        Try
            grdstartistic.Rows.Clear()
            cmd.Connection = cn
            cmd.CommandText = "select * from tblinvoice"
            Dim rd As SqlDataReader = cmd.ExecuteReader
            While rd.Read = True
                Me.grdstartistic.Rows.Add(rd(0), rd(1), rd(2), rd(3), rd(4))
            End While
            rd.Close()
        Catch ex As Exception
        End Try
    End Sub
#End Region

    Private Sub startistic_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        refreshdata()
        totalAmount()
        Main.lblheader.Text = "ស្ថិតិនៃការលក់"
    End Sub

#Region " Date Between"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            grdstartistic.Rows.Clear()
            cmd.Connection = cn
            cmd.CommandText = "select * from tblinvoice where date between'" & startdate.Value & "' and '" & enddate.Value & "'"
            Dim dr As SqlDataReader = cmd.ExecuteReader
            While dr.Read = True
                Me.grdstartistic.Rows.Add(dr(0), dr(1), dr(2), dr(3), dr(4))
            End While
            dr.Close()
            totalAmount()

        Catch ex As Exception
        End Try
    End Sub
#End Region

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim frm As New Sellreport
        frm.ShowDialog()
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            If Me.grdstartistic.SelectedRows.Count = 0 Then
                Exit Sub
            End If
            If MessageBox.Show("Are you want to delete", "Delete Confirme", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                Exit Sub
            End If
            For Each row In grdstartistic.SelectedRows
                Dim code = row.Cells("Column1").Value
                cmd.Connection = cn
                cmd.CommandText = "delete from tblinvoice where InvoiceID= " & code
                cmd.ExecuteNonQuery()
                startistic_Load(sender, e)
            Next
            refreshdata()
        Catch ex As Exception
        End Try
    End Sub
End Class